<?php

namespace Trois\Crontab;

use App\HttpController\Open\WorkWeixinDsf\Event\AccessTokenRefresh;
use App\HttpController\Open\WorkWeixinDsf\Event\SuiteTicketEvent;
use EasySwoole\Component\Singleton;
use EasySwoole\Crontab\JobInterface;
use EasySwoole\EasySwoole\Logger;


/**
 * 刷新企业的access_token
 */
class WorkWechatDsfAccessTokenRefreshCrontab implements JobInterface
{
    use Singleton;

    public function jobName(): string
    {
        return "WorkWechatDsfAccessTokenRefreshCrontab";
    }

    public function crontabRule(): string
    {
        return '0 */3 * * *';
    }

    public function run(): void
    {
        $this->handle();
    }

    public function handle(): bool
    {

        Logger::getInstance()->info("开始处理 WorkWechatDsfAccessTokenRefreshCrontab");

        $suites = \EasySwoole\EasySwoole\Config::getInstance()->getConf('WorkWeixinDsf');

        foreach ($suites as $suite) {

            if (!$suite['IsWork']) {
                continue;
            }

            $AccessTokenRefresh = new AccessTokenRefresh(new SuiteTicketEvent($suite));
            $AccessTokenRefresh->handle();
        }

        return true;
    }


    public function onException(\Throwable $throwable)
    {
        Logger::getInstance()->error("WorkWechatDsfAccessTokenRefreshCrontab onException:" + $throwable->getMessage());
    }

}